package com.ogilvy.audi.model;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.builder.ToStringBuilder;
import org.apache.commons.lang.builder.ToStringStyle;
import org.apache.struts2.json.annotations.JSON;
import org.springframework.security.access.ConfigAttribute;
import org.springframework.security.core.GrantedAuthority;

import javax.persistence.*;
import java.util.*;

/**
 * Created by IntelliJ IDEA.
 * User: User
 * Date: 2009-3-26
 * Time: 13:51:56
 * To change this template use File | Settings | File Templates.
 * ???
 */
@Entity
@Table(name = "TBL_RESOURCE")
@org.hibernate.annotations.Table(appliesTo = "TBL_RESOURCE", comment = " 系统资源表 ")
//@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
public class Resource extends BaseObject {

    private Long id;

  private String name;
    private String type;

    private String value;

    private String displayName;

    private String status;

    private Integer compositor;

    private String whetherLog;

    private Integer version;
    private GeneAttribute geneAttribute = new GeneAttribute();

    private Set<Role> roles = new HashSet();

    /**
     * The default constructor
     */
    public Resource() {

    }


    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
//	@SequenceGenerator(name = "resource_seq", sequenceName = "TBL_resource_seq", allocationSize = 1)
//	@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "resource_seq")
@Column(name = "NUMID")
public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

      @Column(length = 300,name="VC2NAME")
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @ManyToMany(targetEntity = com.ogilvy.audi.model.Role.class, fetch = FetchType.EAGER)
    //@ManyToMany(fetch = FetchType.EAGER,mappedBy = "resources")
//@JoinTable(name = "TBL_REL_ROLE_RESOURCE", joinColumns = @JoinColumn(name = "resource_id"), inverseJoinColumns = @JoinColumn(name = "role_id"))
    //@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
          @JoinTable(
            name = "TBL_REL_ROLE_RESOURCE",
            joinColumns = { @JoinColumn(name = "RESOURCE_ID") },
            inverseJoinColumns = @JoinColumn(name = "ROLE_ID")
    )
    @JSON(serialize = false)
    public Set<Role> getRoles() {
        return roles;
    }

    public void setRoles(Set<Role> roles) {
        this.roles = roles;
    }

    @Column(length = 64, name = "VC2TYPE")
    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    @Column(name = "VC2WHETHERLOG")
    public String getWhetherLog() {
        if (null == whetherLog)
            whetherLog = "false";
        return whetherLog;
    }

    public void setWhetherLog(String whetherLog) {
        this.whetherLog = whetherLog;
    }

    @Column(length = 300, name = "VC2VALUE")
    public String getValue() {
        return value;
    }

    public void setValue(String value) {
        this.value = value;
    }

    @Embedded
   // @SearchableComponent
    public GeneAttribute getGeneAttribute() {
        return geneAttribute;
    }

    public void setGeneAttribute(GeneAttribute geneAttribute) {
        this.geneAttribute = geneAttribute;
    }

    @Column(length = 15, name = "NUMCOMPOSITOR")
    public Integer getCompositor() {
        return compositor;
    }

    public void setCompositor(Integer compositor) {
        this.compositor = compositor;
    }

    @Column(length = 300, name = "VC2DISPLAYNAME")
    public String getDisplayName() {
        return displayName;
    }

    public void setDisplayName(String displayName) {
        this.displayName = displayName;
    }

    @Column(length = 20, name = "VC2STATUS")
    public String getStatus() {
        return status;
    }

    public void setStatus(String status) {
        this.status = status;
    }

    @Version
    public Integer getVersion() {
        return version;
    }

    public void setVersion(Integer version) {
        this.version = version;
    }

    public void addRole(Role role) {
        if (!getRoles().contains(role))
            roles.add(role);
    }


    @Transient
    @JSON(serialize = false)
    public String getRoleAuthorities() {
        List<String> roleAuthorities = new ArrayList<String>();
        for (Role role : roles) {
            roleAuthorities.add(role.getName());
        }
        return StringUtils.join(roleAuthorities, ",");
    }

    @Transient
    @JSON(serialize = false)
    public List<LabelValue> getRoleList() {
        List<LabelValue> userRoles = new ArrayList<LabelValue>();
        if (this.roles != null) {
            for (Role role : roles) {
                // convert the user's roles to LabelValue Objects
                userRoles.add(new LabelValue(role.getDisplayName(), role.getName()));
            }

        }
        return userRoles;
    }

//      @Transient
//     public Set<GrantedAuthority> getAuthorities() {
//        Set<GrantedAuthority> authorities = new LinkedHashSet<GrantedAuthority>();
//        authorities.addAll(roles);
//        return authorities;
//    }

       @Transient
    public Collection<ConfigAttribute> getAuthorities() {
        List<ConfigAttribute> list=new ArrayList();
          for(ConfigAttribute role:roles){
           //   System.out.println("The role is "+role);
                 list.add(role);
          }
       return list;
    }

    public String toString() {
        ToStringBuilder sb = new ToStringBuilder(this, ToStringStyle.DEFAULT_STYLE)
                .append("value", this.value);
        return sb.toString();  //To change body of implemented methods use File | Settings | File Templates.
    }

    public boolean equals(Object o) {
        return false;  //To change body of implemented methods use File | Settings | File Templates.
    }

    public int hashCode() {
        return 0;  //To change body of implemented methods use File | Settings | File Templates.
    }
}
